package com.benmei.weike.web;

import com.benmei.weike.common.Constants;
import com.benmei.weike.common.Page;
import com.benmei.weike.common.filter.RequestPage;
import com.benmei.weike.entity.McBuyCourse;
import com.benmei.weike.service.McCourseService;
import com.benmei.weike.service.common.MemcachedService;
import com.benmei.weike.util.JsonUtil;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.member.TdNtMember;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author: chenhong
 * Date  : 2018/10/13 16:34
 * Description :
 */

@Controller
public class McStudentController {
    //日志
    public static final Logger logger = LoggerFactory.getLogger(McStudentController.class);

    @Autowired //系统课课程service
    private McCourseService mcCourseService;

    @Autowired  //缓存service
    private MemcachedService memcachedService;



    /**
     * 查询学生端--我的系统课课程表（包括课程名称，上一节课时间，课程进度)
     * @param reqPage
     * @param token
     * @param version
     * @return
     */
    @RequestMapping(value = "/studet/token/my/mcCourse", method = {RequestMethod.POST},consumes = "application/json")
    @ResponseBody
    public RetInfo getCourses(@RequestBody RequestPage reqPage,
                              @RequestHeader(value = "token", required = false) String token,
                              @RequestHeader(value = "version", required = false) Integer version) {
        try {
            //打印入参
            logger.info(Constants.prefix_in+ JsonUtil.toJson(reqPage));

            //校验token
            if(StringUtils.isBlank(token)){
                RetInfo retInfo= RetInfo.getClientErrorInfo("参数 token 不能为空");
                logger.info(JsonUtil.toJson(retInfo));
                return retInfo;
            }
            //根据token获取学生信息
            TdNtMember member = (TdNtMember)(memcachedService.get(token));
            reqPage.setObj(member.getMemb_id());
            //获取已购买课程信息
            List<McBuyCourse> list=mcCourseService.list(reqPage);

            Page page=new Page(reqPage.getCurrentPage(),reqPage.getPageSize(),reqPage.getTotalRecord());
            page.setData(list);
            RetInfo retInfo= RetInfo.getSuccessInfo();
            //把课程信息放入retInfo中
            Map<String,Object> resultData=new HashMap<>();
            resultData.put("totalPage",page.getTotalPage());
            resultData.put("studentSources",list);
            retInfo.setObj(resultData);

            //打印出参
            logger.info(Constants.prefix_out+ JsonUtil.toJson(retInfo));
            return retInfo;
        }catch (Exception e){
            //打印错误日志
            logger.error(e.getMessage(),e);
            //打印出参
            RetInfo retInfo= RetInfo.getServerErrorInfo();
            logger.error(Constants.prefix_out+ JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
}
